package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by JINPENG on 2018/10/7.
 */
@Controller
@RequestMapping("/manege/category")
public class CategoryManageController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    /**
     *
     * 新增品类
     * */
    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
       if (user == null){
           return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
       }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //是管理员增加我们处理分类的逻辑
            return  iCategoryService.addCategory(categoryName,parentId);
        }else{
            return  ServerResponse.createByErrorMessage("无操作权限，需要管理员权限");
        }
    }

    /**
     * 更新品类
     *
     * */
    @RequestMapping("set_categoryName.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //是管理员 增加我们处理分类的逻辑
            return  iCategoryService.updateCategoryName(categoryId,categoryName);
        }else{
            return  ServerResponse.createByErrorMessage("无操作权限，需要管理员权限");
        }

    }
    /**
     * 获取品类子节点(平级)
     *
     * */
    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //查询子节点信息。并且不递归
            return  iCategoryService.getChildrenParallelCategory(categoryId);
        }else{
            return  ServerResponse.createByErrorMessage("无操作权限，需要管理员权限");
        }

    }

    /**
     * 获取当前分类id及递归子节点
     *
     * */
    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getDeepCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return  ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        if (iUserService.checkAdminRole(user).isSuccess()){
            //查询当前节点信息。和子节点递归
            return  iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else{
            return  ServerResponse.createByErrorMessage("无操作权限，需要管理员权限");
        }

    }

}
